from moviepy.video.io.VideoFileClip import VideoFileClip
import threading


def split_video(video_path, num_parts):
    # 加载视频文件
    video = VideoFileClip(video_path)

    # 获取视频总时长
    duration = video.duration

    # 计算每段的时长
    part_duration = duration / num_parts

    for i in range(num_parts):
        thread = threading.Thread(target=vd_spit(i, part_duration, video))
        thread.start()

    print(f"视频已成功分割成 {num_parts} 份")


def vd_spit(i, part_duration, video):
    start_time = i * part_duration
    end_time = start_time + part_duration
    # 截取每一段并保存
    part = video.subclip(start_time, end_time)
    part.write_videofile(f"part_{i + 1}.mp4", codec="libx264")


# 调用函数，将视频分割成 4 份
split_video("C:/Users/summit/Videos/Captures/2.mp4", 4)
